﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using BAL;
using VO;
using System.Data.SqlClient;
namespace BTL
{

    public partial class frmHoaDon : Form
    {
        VO.HoadonVO _hdvo;
        DAL.HoaDonDAL _hddal;
        BAL.HoaDonBAL _hdbal;
        public frmHoaDon()
        {
            InitializeComponent();
            cbsearch.Text = "Mã hóa đơn";
        }
        private void data()
        {
            _hdvo = new HoadonVO();
            _hdvo.MaDG = box_madg.Text;
            _hdvo.MaHD = box_mahd.Text;
            _hdvo.MaNV = box_manv.Text;
           // _hdvo.Gia = textBoxX1.Text;
           // _hdvo.MaSach = box_masach.Text;
            _hdvo.NgayMuon = Convert.ToDateTime(box_ngaymuon.Text);
            _hdvo.NgayTra = Convert.ToDateTime(box_ngaytra.Text);
        }
        private void bt_them_Click(object sender, EventArgs e)
        {
            try
            {
                data();
                _hddal = new DAL.HoaDonDAL();
                _hddal._order_them(_hdvo);
                
                MessageBox.Show("Them hoa don thanh cong!", "Thanh Cong!", MessageBoxButtons.OK);
                
            }
            catch
            {
                MessageBox.Show("loi!", "Them hoa don that bai!, Xem help", MessageBoxButtons.OK);
            }
            buttonX1_Click(sender, e);
        }
        
        private void frmHoaDon_Load(object sender, EventArgs e)
        {
            _hdbal = new BAL.HoaDonBAL();
            box_madg.DataSource = _hdbal.hd_madg();
            box_madg.DisplayMember = "MaDG";
            box_manv.DataSource = _hdbal.hd_manv();
            box_manv.DisplayMember = "MaNV";
            buttonX1_Click(sender, e);
            Hoadondatabinding();

        }

        private void buttonX1_Click(object sender, EventArgs e)
        {
            _hdbal = new HoaDonBAL();
            dataGridViewHoaDon.DataSource = _hdbal.hd_xem();
        }
        private void Hoadondatabinding()
        {
            box_mahd.DataBindings.Clear();
            box_mahd.DataBindings.Add("text", dataGridViewHoaDon.DataSource,"MaHD");
            box_madg.DataBindings.Clear();
            box_madg.DataBindings.Add("text", dataGridViewHoaDon.DataSource, "MaDG");
           // box_masach.DataBindings.Clear();
            //box_masach.DataBindings.Add("text", dataGridViewHoaDon.DataSource, "MaSach");
            box_manv.DataBindings.Clear();
            box_manv.DataBindings.Add("text", dataGridViewHoaDon.DataSource, "MaNV");
            box_ngaymuon.DataBindings.Clear();
            box_ngaymuon.DataBindings.Add("text", dataGridViewHoaDon.DataSource, "NgayMuon");
            box_ngaytra.DataBindings.Clear();
            box_ngaytra.DataBindings.Add("text", dataGridViewHoaDon.DataSource, "NgayTra");
            
        }

        private void bt_sua_Click(object sender, EventArgs e)
        {
            try
            {
                data();
                _hddal = new DAL.HoaDonDAL();
                _hddal._order_edit(_hdvo);
                buttonX1_Click(sender, e);
                MessageBox.Show("sua hoa don thanh cong!", "Thanh Cong!", MessageBoxButtons.OK);
            }
            catch
            {
                MessageBox.Show("loi!", "sua hoa don that bai!", MessageBoxButtons.OK);
            }
        }

        private void bt_xoa_Click(object sender, EventArgs e)
        {
            try
            {
                _hddal = new DAL.HoaDonDAL();
                data();
                _hddal._order_delete(box_mahd.ToString());
                buttonX1_Click(sender, e);
                MessageBox.Show("xoa hoa don thanh cong!", "Thanh Cong!", MessageBoxButtons.OK);
            }
            catch
            {
                MessageBox.Show("loi!", "xoa hoa don that bai!", MessageBoxButtons.OK);
            }
        }

        private void bt_timkiem_Click(object sender, EventArgs e)
        {
            if (cbsearch.Text == "Mã hóa đơn")
            {
                SqlConnection conn = new SqlConnection(Core.Connectionstring.Con.ToString());
                SqlCommand cmd = new SqlCommand();
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                DataTable dt = new DataTable();
                cmd.Connection = conn;
                conn.Open();
                cmd.CommandType = CommandType.Text;
                cmd.CommandText = "select * from HoaDon where MaHD like '%" + tbtk.Text.Trim() + "%'";
                da.Fill(dt);
                dataGridViewHoaDon.DataSource = dt;
                conn.Close();
            }
            if (cbsearch.Text == "Mã Độc Giả")
            {
                SqlConnection conn = new SqlConnection(Core.Connectionstring.Con.ToString());
                SqlCommand cmd = new SqlCommand();
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                DataTable dt = new DataTable();
                cmd.Connection = conn;
                conn.Open();
                cmd.CommandType = CommandType.Text;
                cmd.CommandText = "select * from HoaDon where MaDG like '%" + tbtk.Text.Trim() + "%'";
                da.Fill(dt);
                dataGridViewHoaDon.DataSource = dt;
                conn.Close();
            }
        }

        private void dataGridViewHoaDon_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyData == Keys.F1)
            {
                frmHelp _help = new frmHelp();
                _help.Show();
            }
        }

        private void buttonX2_Click(object sender, EventArgs e)
        {
                frmHelp _help = new frmHelp();
                _help.Show();
        }

        private void buttonX3_Click(object sender, EventArgs e)
        {
            ChiTietHD _cthd = new ChiTietHD();
            _cthd.Show();
        }

    }
}
